import { Component } from "cc";
import { AudioMusic } from "./AudioMusic";
import { AudioEffectPool } from "./AudioEffectPool";
import { hiyooo } from "../../HiYOOO";
import { AudioClip } from "cc";
import { get } from "http";

const LOCAL_STORE_KEY = "hiyooo_audio_data";
/**
 * 音乐 & 音效管模块
 * @example 
 * hiyooo.audio.playMusic("bgm_1")
 */
export class AudioManager extends Component {
    //背景音乐
    public music:AudioMusic = null!;
    //单效 pool (音效播放池)
    public effect:AudioEffectPool = new AudioEffectPool();

    private local_data:any = {}

    //设置背景音乐播放回调
    public setMusicComplete(callback:Function | null = null):void{
        this.music.onCompleted = callback;
    }

    /***
     * 播放背景音乐
     * @param url 音乐资源路径
     * @param callback 音乐播放完成回调
     * @param bundleName 资源包名
     */
    public playMusic(url:string,callback?:Function,bundleName?:string):void{
        if(!this.music.switch) return;
        this.music.loop = false;
        this.music.load(url,callback,bundleName).then();    
    }

    /**
     * 循环播放背景音乐
     * @param url 音效资源路径
     * @param callback 播放完成回调
     * @param bundleName 资源包名
     */
    public playMusicLoop(url:string,callback?:Function,bundleName?:string):void{
        if (!this.music.switch) return;
        this.music.loop = true;
        this.music.load(url,callback,bundleName).then();
    }

    /**
     * 暂停背景音乐
     */
    public stopMusic():void{
        if (this.music.switch && this.music.playing) {
            this.music.stop();
        }
    }

    /**
     * 获取背景音乐播放进度
     * @returns 进度值 0-1
    */
    get progressMusic():number{
        return this.music.progerss;
    }
    //设置背景音乐播放进度
    set progerssMusic(value:number){
        this.music.progress = value;
    }

    //获取背景音乐音量
    get musicVolume():number{
        return this.music.volume;
    }

    //设置背景音乐音量
    set musicVolume(value:number){
        this.music.volume = value;
        this.save();
    }

    //获取音乐开关
    get switchMusic():boolean{
        return this.music.switch;
    }

    //设置背景音乐开关
    set switchMusic(value:boolean){
        this.music.switch = value;
        if(!value) this.music.stop();
        this.save();
    }

    //播放音效
    public playEffectd(url:string | AudioClip,bundleName?:string,onPlayCompleted?:Function):Promise<number>{
        return this.effect.load(url,bundleName,onPlayCompleted);
    }

    //回收音效
    public putEffect(aeid:number,url:string | AudioClip,bundleName?:string){
        this.effect.put(aeid,url,bundleName);
    }


    //获取音效音量
    get volumeEffect():number{
        return this.effect.volume;
    }

    //设置音效音量
    set volumnEffect(value:number){
        this.effect.volume = value;
        this.save();
    }

    //获取音效开关
    get switchEffect():boolean{
        return this.effect.switch;
    }

    //设置音效开关
    set switchEffect(value:boolean){
        this.effect.switch = value;
        if (!value) {
            this.effect.stop()
        }
        this.save();
    }

    //恢复所有音乐和音效
    public resumeAll():void{
        if (!this.music.playing && this.music.progress > 0) {
            this.music.play();
        }
        this.effect.play();
    }

    //暂停当前所有音乐和音效
    public pauseAll():void{
        if (this.music.playing) {
            this.music.pause();
        }
    
        this.effect.pause();
    }

    //停止所有音乐和音效
    public stopAll():void{
        this.music.stop();
        this.effect.stop();
    }


    public load():void{
        this.music  = this.getComponent(AudioMusic) || this.addComponent(AudioMusic)!;
        this.local_data = hiyooo.storage.get(LOCAL_STORE_KEY);
        if (this.local_data) {
            try {
                this.setState();
            } catch (error) {
                this.setStateDefault();
            }
        }else{
            this.setStateDefault();
        }

    }

    public setState():void{
        this.music.volume = this.local_data.volume_music;
        this.music.switch = this.local_data.switch_music;
        this.effect.volume  = this.local_data.volume_effect;
        this.effect.switch = this.local_data.switch_effect;
    }

    public setStateDefault():void{
        this.local_data = {};
        this.music.volume = 1;
        this.music.switch = true;
        this.effect.volume = 1;
        this.effect.switch = true;
    }



    //保存音乐设置
    public save():void{
        this.local_data.volume_music = this.music.volume;
        this.local_data.volume_effect = this.effect.volume;
        this.local_data.switch_music = this.music.switch;
        this.local_data.switch_effect = this.effect.switch;
        hiyooo.storage.set(LOCAL_STORE_KEY,this.local_data);
    }






}